package cn.lyn4ever.goods.web.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.lyn4ever.common.MyApiErrors;
import cn.lyn4ever.entity.GoodsSku;
import cn.lyn4ever.entity.GoodsSpu;
import cn.lyn4ever.goods.web.common.GoodsVal;
import cn.lyn4ever.goods.web.mapper.GoodsSkuDao;
import cn.lyn4ever.goods.web.mapper.GoodsSpuDao;
import cn.lyn4ever.goods.web.service.GoodsSkuService;
import cn.lyn4ever.tool.Assert;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 商品的sku表，每一个商品对应多个sku表(GoodsSku)表服务实现类
 *
 * @author makejava
 * @since 2020-03-26 22:55:31
 */
@Service("goodsSkuService")
public class GoodsSkuServiceImpl implements GoodsSkuService {
    @Resource
    private GoodsSkuDao goodsSkuDao;
    @Resource
    private GoodsSpuDao goodsSpuDao;

    /**
     * 根据商品spuId获取商品sku
     *
     * @param SpuId 商品spuId
     * @return
     */
    @Override
    public List<GoodsSku> getGoodsSkuBySpuId(Integer SpuId) {

        return goodsSkuDao.getGoodsSkuBySpuId(SpuId);
    }

    /**
     * 根据skuid查询出该商品的详细信息
     *
     * @param skuid
     * @return
     */
    @Override
    public Map<String, Object> getOne(Integer skuid) {
        GoodsSku sku = goodsSkuDao.getOneById(skuid);
        Assert.NotNull(sku, MyApiErrors.FAILED);
        if (!sku.getZt().equals(GoodsVal.SKU_ZT_WELL)) {
            return null;
        }
        GoodsSpu spu = goodsSpuDao.getOneById(sku.getSpuId());
        Assert.NotNull(spu, MyApiErrors.FAILED);
        Map<String, Object> skumap = BeanUtil.beanToMap(sku);
        skumap.putAll(BeanUtil.beanToMap(spu));

        return skumap;
    }


}